import Vue from 'vue'
import Router from 'vue-router'
import getDeviceInfo from './common/deviceInfo'
// import store from './store'
// import * as events from './store/events'
// import consts from './common/consts'
// import axios from 'axios'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  scrollBehavior() {
    return {
      x: 0,
      y: 0
    }
  },
  routes: [
    // 主页
    {
      path: '/',
      name: 'index',
      component: resolve => require(['@/views/index/Index'], resolve),
      meta: {
        needCatid: true
      }
    },
    // test
    {
      path: '/test',
      name: 'test',
      component: resolve => require(['@/views/Test'], resolve),
      meta: {
        needCatid: true
      }
    },
    // 不是微信客户端的页面
    {
      path: '/noWechatPage',
      name: 'noWechatPage',
      component: resolve => require(['@/views/index/Index'], resolve),
      meta: {
        needCatid: true
      }
    },
    // 品牌详情
    {
      path: '/companyDetails/:itemid',
      name: 'companyDetails',
      component: resolve => require(['@/views/company/CompanyDetails'], resolve)
    },
    // 品牌详情
    {
      path: '/companyDetails/:itemid',
      name: 'companyDetails',
      component: resolve => require(['@/views/company/NewCompany'], resolve)
    },
    // 添加品牌
    {
      path: '/addbrand',
      name: 'addbrand',
      component: resolve => require(['@/views/addBrand/AddBrand'], resolve),
      meta: {
        needCatid: true
      }
    },
    // 添加成功
    {
      path: '/addsuccess/:catid',
      name: 'addsuccess',
      component: resolve => require(['@/views/addBrand/AddSuccess'], resolve),
      meta: {
        needCatid: true
      }
    },
    // 奖牌证书领取页
    {
      path: '/applyReward/:itemid',
      name: 'applyReward',
      component: resolve => require(['@/views/applyReward/ApplyReward'], resolve),
      meta: {
        needCatid: true
      }
    },
    // 奖牌证书信息提交成功页
    {
      path: '/applyRewardSuccess',
      name: 'applyRewardSuccess',
      component: resolve => require(['@/views/applyReward/RewardSuccess'], resolve)
    },
    // 信息异常
    {
      path: '/infoError/:catid',
      name: 'infoError',
      component: resolve => require(['@/views/infoError/InfoError'], resolve),
      meta: {
        needCatid: true
      }
    },
    // 意见反馈
    {
      path: '/feedBack',
      name: 'feedBack',
      component: resolve => require(['@/views/feedBack/FeedBack'], resolve),
      meta: {
        needCatid: true
      }
    },
    // 意见反馈
    {
      path: '/feedBackSuccess',
      name: 'feedBackSuccess',
      component: resolve => require(['@/views/feedBack/feedBackSuccess'], resolve),
      meta: {
        needCatid: true
      }
    },
    // 帮助中心
    {
      path: '/helpCenter',
      name: 'helpCenter',
      component: resolve => require(['@/views/helpCenter/HelpCenter'], resolve),
      meta: {
        needCatid: true
      }
    },
    // 视频演示
    {
      path: '/teachVideo',
      name: 'teachVideo',
      component: resolve => require(['@/views/helpCenter/TeachVideo'], resolve)
    },
    // 投票规则
    {
      path: '/voteRules/:catid',
      name: 'voteRules',
      component: resolve => require(['@/views/helpCenter/VoteRules'], resolve),
      meta: {
        needCatid: true
      }
    },
    // 分享教程
    {
      path: '/shareDemo',
      name: 'shareDemo',
      component: resolve => require(['@/views/helpCenter/ShareDemo'], resolve)
    },
    // 流程
    {
      path: '/process',
      name: 'process',
      component: resolve => require(['@/views/helpCenter/Process'], resolve)
    },
    // catid 不正确的页面
    {
      path: '/catidNotRightPage',
      name: 'catidNotRightPage',
      component: resolve => require(['@/views/catidNotRightPage/CatidNotRightPage'], resolve)
    },
    // 没有 catid 的页面     4期开始调整为首页
    {
      path: '/homePage',
      name: 'homePage',
      component: resolve => require(['@/views/homePage/HomePage'], resolve)
    },
    // 成功案例
    {
      path: '/successfulCase',
      name: 'successfulCase',
      component: resolve => require(['@/views/homePage/SuccessfulCase'], resolve)
    },
    // 品牌名片
    {
      path: '/brandNameCard',
      name: 'brandNameCard',
      component: resolve => require(['@/views/homePage/BrandNameCard'], resolve)
    },
    // 成功案例详情
    {
      path: '/successfulCaseDetail/:catid',
      name: 'successfulCaseDetail',
      component: resolve => require(['@/views/homePage/SuccessfulCaseDetail'], resolve),
      meta: {
        needCatid: true
      }
    },
    // 活动宗旨
    {
      path: '/purposeOfActivity',
      name: 'purposeOfActivity',
      component: resolve => require(['@/views/homePage/purposeOfActivity'], resolve)
    },
    // 品牌名片详情
    {
      path: '/brandNameCardDetail/:itemid',
      name: 'brandNameCardDetail',
      component: resolve => require(['@/views/homePage/brandCardDetail'], resolve)
    },
    // 投票结束
    {
      path: '/theEndPage',
      name: 'theEndPage',
      redirect: '/newEndPage'
    },
    // 媒体转发
    {
      path: '/mediaForward',
      name: 'mediaForward',
      component: resolve => require(['@/views/mediaForward/MediaForward'], resolve)
    },
    // 新榜单页
    {
      path: '/newEndPage',
      name: 'newEndPage',
      component: resolve => require(['@/views/newEndPage/newEndPage'], resolve)
    },
    // 重新定向投票结束
    {
      path: '/oldEndPage',
      name: 'oldEndPage',
      component: resolve => require(['@/views/end/oldEndPage'], resolve),
      meta: {
        needCatid: true
      }
    },
    // 活动
    {
      path: '/nationalday',
      name: 'nationalday',
      component: resolve => require(['@/views/activityPage/NationalDay'], resolve)
    }
  ]
})
const notNeedFromIndex = ['index', 'theEndPage', 'oldEndPage', 'newEndPage', 'successfulCaseDetail', 'nationalday'] // 进入这些页面时 不用判断是否是从首页进来
router.beforeEach((to, from, next) => {
  const catid = to.query.catid || to.params.catid
  const itemid = to.query.itemid || to.params.itemid
  // 判断是不是从首页进来的 不是从首页进来的 进入首页去 如果url里有catid才跳 没有的就不管了
  if (notNeedFromIndex.indexOf(to.name) === -1 && !window.sessionStorage.getItem('fromIndex') && catid) {
    // 判断是否是修改品牌链接
    if ((to.name === 'addbrand' && itemid) || to.name === 'addsuccess') {
      next()
    } else {
      window.sessionStorage.setItem('replaced', true)
      next({
        path: '/?catid=' + catid,
        replace: true
      })
    }
    return
  }
  if (to.meta.needCatid) {
    // 需要判断有没有catid
    // 通过meta中的neecatid判断当前页面是否需要catid
    // 如果需要catid 但是没有 要跳到homepage 首页 pc端跳到pc首页
    if (catid) {
      next()
    } else {
      if (getDeviceInfo().isPhone) {
        window.sessionStorage.setItem('replaced', true)
        next({
          path: '/homePage',
          replace: true
        })
      } else {
        window.location.href = 'http://www.brandzw.com'
      }
    }
  } else {
    next()
  }
})
router.afterEach(() => {
  window.scrollTo(0, 0)
})
export default router